package com.hup.sidebar.model;

import android.accessibilityservice.AccessibilityService;
import android.graphics.Bitmap;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.annotation.JSONField;
import com.hup.sidebar.constant.PieFunctionConstant;

import java.util.Objects;

import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 功能对象
 *
 * @author hugan
 * @date 2022/6/3
 */
@Data
@NoArgsConstructor
public class PieFunction {

    /**
     * 类型
     */
    public int type;
    /**
     * 文本描述,或唯一的命令标志,或
     * {@link PieFunctionConstant#TYPE_APP} 时,是app的[packageName]
     */
    public String command;
    /**
     * 图标的资源id,nullable
     */
    @JSONField(serialize = false)
    public Integer iconId;

    /**
     * {@link PieFunctionConstant#TYPE_KEY} 时不为空
     * 如 {@link AccessibilityService#GLOBAL_ACTION_BACK}
     */
    @JSONField(serialize = false)
    public Integer keyAction;

    /**
     * 缓存图标,nullable
     * 仅在全局使用的function对象,才能使用此字段!避免内存泄露
     */
    @JSONField(serialize = false)
    public Bitmap cachedIcon;

    public PieFunction(int type, String command) {
        this.type = type;
        this.command = command;
    }

    public static PieFunction key(String command, int iconId, int keyAction) {
        PieFunction res = new PieFunction();
        res.type = PieFunctionConstant.TYPE_KEY;
        res.command = command;
        res.iconId = iconId;
        res.keyAction = keyAction;
        return res;
    }

    public boolean isSame(PieFunction obj) {
        return type == obj.type && Objects.equals(command, obj.command);
    }

    @NonNull
    @Override
    public String toString() {
        return "PieFunction{" +
                "type=" + type +
                ", command='" + command + '\'' +
                '}';
    }

    public PieFunction copy() {
        PieFunction res = new PieFunction();
        res.type = type;
        res.command = command;
        res.keyAction = keyAction;
        res.iconId = iconId;
        return res;
    }

}
